package stud.mpgame.flash.test;

import flash.utils.IDataInput;

import stud.mpgame.network.Message;

import stud.mpgame.snapshot.ISnapshot;
import stud.mpgame.snapshot.ASnapshot;
import stud.mpgame.snapshot.ICommand;
import stud.mpgame.snapshot.IGameObject;

import stud.mpgame.utils.ICloneable;

import stud.mpgame.flash.net.gameobject.TestGameObject;

/**
 * ...
 * @author pavel[f.pavel91@gmail.com]
 */
class TestSnapshot extends ASnapshot, implements ICloneable {
	
	private var testGameObject:TestGameObject;
	//private var time:Int;
	//private var lastCommandApplyTime:Int;
	
	public function new(value:Int, time:Int) {
		super();
		this.testGameObject = new TestGameObject(value);
		this.time = time;
	}	
	
	public override function getTime():Int {
		return time;
	}	
	
	public function getValue():Int {
		return testGameObject.getValue();
	}
	
	public function setValue(value:Int):Void {
		testGameObject.setValue(value);
	}
	
	public override function setTime(time:Int):Void {
		this.time = time;
	}
	
	public override function interpolate(deltaTime:Int, serverSnapshot:ISnapshot):Void {
		super.interpolate(deltaTime, serverSnapshot);
		time += deltaTime;
	}
	
	public override function extrapolate(deltaTime:Int):Void {
		super.extrapolate(deltaTime);
		time += deltaTime;
	}
	
	public override function synchronize(serverSnapshot:ISnapshot):Void {
		//??
	}
	
    public override function readExternal(input:IDataInput, message:Message):Void {
        lastCommandApplyTime = input.readInt(); //TODO, super.writeExternal();
		time = input.readInt();
		testGameObject.setValue(input.readInt());	
    }
	
	public function getClone():ICloneable {
		var clone:TestSnapshot = new TestSnapshot(testGameObject.getValue(), time);
		return clone;
	}
	
	public override function toString():String {
		var result:String = "SNAPSHOT: TIME = " + getTime() + " VALUE = " + getValue();
		return result;
	}
	
	public override function getAllGameObjects():List< IGameObject > {
		var list:List< IGameObject > = new List< IGameObject >();
		list.add(testGameObject);
		return list;
	}
	
	public override function getGameObject(id:Int):IGameObject {
		return testGameObject;
	}
}